package com.jinlia.show.modules.system.service;

import com.jinlia.show.modules.base.constant.*;
import com.jinlia.show.modules.base.model.po.ErrandOrderPO;
import com.jinlia.show.modules.base.model.po.OrderPO;
import com.jinlia.show.modules.base.model.query.DeliveryOrderQuery;
import com.jinlia.show.modules.base.model.query.OrderQuery;
import com.jinlia.show.modules.base.model.vo.DeliveryOrderVO;
import com.jinlia.show.modules.base.system.entity.DeliveryOrder;
import com.jinlia.show.modules.base.system.entity.ErrandOrder;
import com.jinlia.show.modules.base.system.entity.Order;
import com.jinlia.show.modules.base.system.service.DeliveryOrderMapperService;
import com.jinlia.show.modules.base.system.service.ErrandOrderMapperService;
import com.jinlia.show.modules.base.system.service.OrderMapperService;
import com.jinlia.show.modules.system.mapper.WorktableDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class WorktableService {
    @Autowired
    DeliveryOrderMapperService deliveryOrderMapperService;
    @Autowired
    OrderMapperService orderMapperService;
    @Autowired
    ErrandOrderMapperService errandOrderMapperService;

    @Autowired
    WorktableDao worktableDao;
    /**
     * 待办销售订单
     *
     * @return
     */
    public List<DeliveryOrderVO> getTodoSaleOrder(DeliveryOrderQuery deliveryOrderQuery) {
        List<DeliveryOrderVO> todoOrder = worktableDao.getTodoOrder(deliveryOrderQuery);
        return todoOrder;
    }
    public List<OrderPO> getTodoSaleOrder2(OrderQuery orderQuery) {
        List<OrderPO> poList = orderMapperService.getBaseMapper().getPOList(orderQuery);
        return poList;
    }

    /**
     * 待办跑腿单
     */
    public List<ErrandOrderPO> getTodoErrandOrder() {
        List<ErrandOrder> list = errandOrderMapperService.lambdaQuery()
                .in(ErrandOrder::getErrandOrderStatus,
                        ErrandOrderStatusEnum.A.getValue(),
                        ErrandOrderStatusEnum.B.getValue(),
                        ErrandOrderStatusEnum.I.getValue())
                .list();
        List<ErrandOrderPO> collect = list.stream().map(ErrandOrderPO::fromEntity).collect(Collectors.toList());
        return collect;
    }

    public List<OrderPO> getTodoConfirmedOrder(OrderQuery orderQuery) {
        List<Order> list = orderMapperService.lambdaQuery()
                .eq(Order::getOrderStatus, OrderStatusEnum.STEP_0.getValue())
                .in(Order::getPayStatus, OrderPayStatusEnum.ALL_PAY.getValue(), OrderPayStatusEnum.PART_PAY.getValue())
                .eq(Order::getEnableStatus, EnableStatusEnum.ENABLE.getValue())
                .orderByAsc(Order::getId)
                .list();
        List<OrderPO> collect = list.stream().map(OrderPO::fromEntity).collect(Collectors.toList());
        //填充Item信息
        orderMapperService.setItems(orderQuery.isNeedSetGoods(), collect);
        return collect;
    }

    public List<OrderPO> getTodoDeliveryOrder(OrderQuery orderQuery) {
        List<Order> list = orderMapperService.lambdaQuery()
                .in(Order::getOrderStatus, OrderStatusEnum.STEP_1.getValue(), OrderStatusEnum.STEP_2.getValue())
                .eq(Order::getEnableStatus, EnableStatusEnum.ENABLE.getValue())
                .orderByAsc(Order::getId)
                .list();
        List<OrderPO> collect = list.stream().map(OrderPO::fromEntity).collect(Collectors.toList());
        //填充Item信息
        orderMapperService.setItems(orderQuery.isNeedSetGoods(), collect);
        return collect;
    }
}
